package demo2;

public class MyCircularQueue1 {
        public int[] elem;
        public int front;//队头下标
        public int rear;//队尾下标
        public int usedSize;
        public MyCircularQueue1(int k) {
            this.elem = new int[k];
        }

        //入列
        public boolean enQueue(int value) {
            //判断有没有满
            if (isFull()) return false;
            this.elem[rear] = value;
            this.rear = (rear + 1) % elem.length;
            usedSize++;
            return true;
        }

        //出列
        public boolean deQueue() {
            if (isEmpty()) return false;
            this.front = (this.front + 1) % this.elem.length;
            usedSize--;
            return true;
        }

        //获得队头元素
        public int Front() {
            if (isEmpty()) return -1;
            return this.elem[this.front];
        }

        //获得队尾元素
        public int Rear() {
            if (isEmpty()) return -1;
            if (rear == 0) {
                return this.elem[elem.length-1];
            } else {
                return this.elem[rear-1];
            }
        }

        public boolean isEmpty() {
            if (this.front == this.rear && usedSize != this.elem.length) {
                return true;
            }
            return false;
        }

        public boolean isFull() {
            return this.usedSize == this.elem.length;
        }
    }

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */

